1   /*
2    * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  import java.util.Arrays;
25  import javax.management.openmbean.ArrayType;
26  import javax.management.openmbean.CompositeData;
27  import javax.management.openmbean.CompositeDataSupport;
28  import javax.management.openmbean.CompositeDataView;
29  import javax.management.openmbean.CompositeType;
30  import javax.management.openmbean.OpenDataException;
31  import javax.management.openmbean.OpenType;
32  import javax.management.openmbean.SimpleType;
33  
34  public interface MerlinMXBean {
35  
36      int PInt = 59;
37      SimpleType PIntType = SimpleType.INTEGER;
38      int getPInt();
39      void setPInt(int x);
40      int opPInt(int x, int y);
41  
42      long PLong = Long.MAX_VALUE;
43      SimpleType PLongType = SimpleType.LONG;
44      long getPLong();
45      void setPLong(long x);
46      long opPLong(long x, long y);
47  
48      short PShort = 55;
49      SimpleType PShortType = SimpleType.SHORT;
50      short getPShort();
51      void setPShort(short x);
52      short opPShort(short x, short y);
53  
54      byte PByte = 13;
55      SimpleType PByteType = SimpleType.BYTE;
56      byte getPByte();
57      void setPByte(byte x);
58      byte opPByte(byte x, byte y);
59  
60      char PChar = 'x';
61      SimpleType PCharType = SimpleType.CHARACTER;
62      char getPChar();
63      void setPChar(char x);
64      char opPChar(char x, char y);
65  
66      float PFloat = 1.3f;
67      SimpleType PFloatType = SimpleType.FLOAT;
68      float getPFloat();
69      void setPFloat(float x);
70      float opPFloat(float x, float y);
71  
72      double PDouble = Double.MAX_VALUE;
73      SimpleType PDoubleType = SimpleType.DOUBLE;
74      double getPDouble();
75      void setPDouble(double x);
76      double opPDouble(double x, double y);
77  
78      boolean PBoolean = true;
79      SimpleType PBooleanType = SimpleType.BOOLEAN;
80      boolean getPBoolean();
81      void setPBoolean(boolean x);
82      boolean opPBoolean(boolean x, boolean y);
83  
84      Integer WInteger = new Integer(59);
85      SimpleType WIntegerType = SimpleType.INTEGER;
86      Integer getWInteger();
87      void setWInteger(Integer x);
88      Integer opWInteger(Integer x, Integer y);
89  
90      Long WLong = new Long(Long.MAX_VALUE);
91      SimpleType WLongType = SimpleType.LONG;
92      Long getWLong();
93      void setWLong(Long x);
94      Long opWLong(Long x, Long y);
95  
96      Short WShort = new Short(Short.MAX_VALUE);
97      SimpleType WShortType = SimpleType.SHORT;
98      Short getWShort();
99      void setWShort(Short x);
100     Short opWShort(Short x, Short y);
101 
102     Byte WByte = new Byte(Byte.MAX_VALUE);
103     SimpleType WByteType = SimpleType.BYTE;
104     Byte getWByte();
105     void setWByte(Byte x);
106     Byte opWByte(Byte x, Byte y);
107 
108     Character WCharacter = new Character('x');
109     SimpleType WCharacterType = SimpleType.CHARACTER;
110     Character getWCharacter();
111     void setWCharacter(Character x);
112     Character opWCharacter(Character x, Character y);
113 
114     Float WFloat = new Float(1.3f);
115     SimpleType WFloatType = SimpleType.FLOAT;
116     Float getWFloat();
117     void setWFloat(Float x);
118     Float opWFloat(Float x, Float y);
119 
120     Double WDouble = new Double(Double.MAX_VALUE);
121     SimpleType WDoubleType = SimpleType.DOUBLE;
122     Double getWDouble();
123     void setWDouble(Double x);
124     Double opWDouble(Double x, Double y);
125 
126     Boolean WBoolean = Boolean.TRUE;
127     SimpleType WBooleanType = SimpleType.BOOLEAN;
128     Boolean getWBoolean();
129     void setWBoolean(Boolean x);
130     Boolean opWBoolean(Boolean x, Boolean y);
131 
132     int[] PIntA = {2, 3, 5, 7, 11, 13};
133     ArrayType PIntAType = ArrayTypeMaker.make(SimpleType.INTEGER, true);
134     int[] getPIntA();
135     void setPIntA(int[] x);
136     int[] opPIntA(int[] x, int[] y);
137 
138     int[][] PInt2D = {{1, 2}, {3, 4}};
139     ArrayType PInt2DType = ArrayTypeMaker.make(1, PIntAType);
140     int[][] getPInt2D();
141     void setPInt2D(int[][] x);
142     int[][] opPInt2D(int[][] x, int[][] y);
143 
144     Integer[] WIntA = {new Integer(3), new Integer(5)};
145     ArrayType WIntAType = ArrayTypeMaker.make(1, SimpleType.INTEGER);
146     Integer[] getWIntA();
147     void setWIntA(Integer[] x);
148     Integer[] opWIntA(Integer[] x, Integer[] y);
149 
150     Integer[][] WInt2D = {{new Integer(3)}, {new Integer(5)}};
151     ArrayType WInt2DType = ArrayTypeMaker.make(2, SimpleType.INTEGER);
152     Integer[][] getWInt2D();
153     void setWInt2D(Integer[][] x);
154     Integer[][] opWInt2D(Integer[][] x, Integer[][] y);
155 
156     String XString = "yo!";
157     SimpleType XStringType = SimpleType.STRING;
158     String getXString();
159     void setXString(String x);
160     String opXString(String x, String y);
161 
162     String[] XStringA = {"hello", "world"};
163     ArrayType XStringAType = ArrayTypeMaker.make(1, SimpleType.STRING);
164     String[] getXStringA();
165     void setXStringA(String[] x);
166     String[] opXStringA(String[] x, String[] y);
167 
168     int[] NoInts = {};
169     ArrayType NoIntsType = ArrayTypeMaker.make(SimpleType.INTEGER, true);
170     int[] getNoInts();
171     void setNoInts(int[] x);
172     int[] opNoInts(int[] x, int[] y);
173 
174     GetSetBean GetSet = GetSetBean.make(5, "x", new String[] {"a", "b"});
175     CompositeType GetSetType =
176         CompositeTypeMaker.make(GetSetBean.class.getName(),
177                                 GetSetBean.class.getName(),
178                                 new String[] {"int", "string", "stringArray"},
179                                 new String[] {"int", "string", "stringArray"},
180                                 new OpenType[] {
181                                     SimpleType.INTEGER,
182                                     SimpleType.STRING,
183                                     ArrayTypeMaker.make(1, SimpleType.STRING),
184                                 });
185     GetSetBean getGetSet();
186     void setGetSet(GetSetBean bean);
187     GetSetBean opGetSet(GetSetBean x, GetSetBean y);
188 
189     GetterInterface Interface = new GetterInterface() {
190         public boolean isWhatsit() {
191             return true;
192         }
193 
194         public int[] getInts() {
195             return new int[] {1};
196         }
197 
198         public String[] getStrings() {
199             return new String[] {"x"};
200         }
201 
202         public GetSetBean getGetSet() {
203             return GetSetBean.make(3, "a", new String[] {"b"});
204         }
205 
206         public boolean equals(Object o) {
207             if (!(o instanceof GetterInterface))
208                 return false;
209             GetterInterface i = (GetterInterface) o;
210             return isWhatsit() == i.isWhatsit() &&
211                    Arrays.equals(getInts(), i.getInts()) &&
212                    Arrays.equals(getStrings(), i.getStrings()) &&
213                    getGetSet().equals(i.getGetSet());
214         }
215      };
216      CompositeType InterfaceType =
217         CompositeTypeMaker.make(GetterInterface.class.getName(),
218                                 GetterInterface.class.getName(),
219                                 new String[] {
220                                     "ints", "getSet", "strings", "whatsit",
221                                 },
222                                 new String[] {
223                                     "ints", "getSet", "strings", "whatsit",
224                                 },
225                                 new OpenType[] {
226                                     ArrayTypeMaker.make(SimpleType.INTEGER, true),
227                                     GetSetType,
228                                     ArrayTypeMaker.make(1, SimpleType.STRING),
229                                     SimpleType.BOOLEAN,
230                                 });
231      GetterInterface getInterface();
232      void setInterface(GetterInterface i);
233      GetterInterface opInterface(GetterInterface x, GetterInterface y);
234 
235     /* Testing that we can use a public no-arg constructor plus a setter
236      * for every getter to reconstruct this object.  Note that the
237      * constructor-guessing logic is no longer valid for this class,
238      * so if we can reconstruct it it must be because of the setters.
239      */
240     public static class GetSetBean {
241         public GetSetBean() {
242             this(0, null, null);
243         }
244 
245         private GetSetBean(int Int, String string, String[] stringArray) {
246             this.Int = Int;
247             this.string = string;
248             this.stringArray = stringArray;
249         }
250 
251         public static GetSetBean
252                 make(int Int, String string, String[] stringArray) {
253             GetSetBean b = new GetSetBean(Int, string, stringArray);
254             return b;
255         }
256 
257         public int getInt() {
258             return Int;
259         }
260 
261         public String getString() {
262             return this.string;
263         }
264 
265         public String[] getStringArray() {
266             return this.stringArray;
267         }
268 
269         public void setInt(int x) {
270             this.Int = x;
271         }
272 
273         public void setString(String string) {
274             this.string = string;
275         }
276 
277         public void setStringArray(String[] stringArray) {
278             this.stringArray = stringArray;
279         }
280 
281         public boolean equals(Object o) {
282             if (!(o instanceof GetSetBean))
283                 return false;
284             GetSetBean b = (GetSetBean) o;
285             return (b.Int == Int &&
286                     b.string.equals(string) &&
287                     Arrays.equals(b.stringArray, stringArray));
288         }
289 
290         String string;
291         String[] stringArray;
292         int Int;
293     }
294 
295     public static interface GetterInterface {
296         public String[] getStrings();
297         public int[] getInts();
298         public boolean isWhatsit();
299         public GetSetBean getGetSet();
300 
301         // We uselessly mention the public methods inherited from Object because
302         // they should not prevent the interface from being translatable.
303         // We could consider encoding the result of hashCode() and toString()
304         // on the original object that implements this interface into the
305         // generated CompositeData and referencing that in the proxy, but
306         // that seems ambitious for now.  Doing it only if hashCode() and/or
307         // toString() are mentioned in the interface is a possibility but
308         // a rather abstruse one.
309         public boolean equals(Object o);
310         public int hashCode();
311         public String toString();
312     }
313 
314     public static class ArrayTypeMaker {
315         static ArrayType make(int dims, OpenType baseType) {
316             try {
317                 return new ArrayType(dims, baseType);
318             } catch (OpenDataException e) {
319                 throw new Error(e);
320             }
321         }
322 
323         static ArrayType make(SimpleType baseType, boolean primitiveArray) {
324             try {
325                 return new ArrayType(baseType, primitiveArray);
326             } catch (OpenDataException e) {
327                 throw new Error(e);
328             }
329         }
330     }
331 
332     public static class CompositeTypeMaker {
333         static CompositeType make(String className,
334                                   String description,
335                                   String[] itemNames,
336                                   String[] itemDescriptions,
337                                   OpenType[] itemTypes) {
338             try {
339                 return new CompositeType(className,
340                                          description,
341                                          itemNames,
342                                          itemDescriptions,
343                                          itemTypes);
344             } catch (OpenDataException e) {
345                 throw new Error(e);
346             }
347         }
348     }
349 
350     public static interface GraphMXBean {
351         public NodeMXBean[] getNodes();
352     }
353 
354     public static class Graph implements GraphMXBean {
355         public Graph(Node... nodes) {
356             for (Node node : nodes)
357                 node.setGraph(this);
358             this.nodes = nodes;
359         }
360 
361         public NodeMXBean[] getNodes() {
362             return nodes;
363         }
364 
365         private final Node[] nodes;
366     }
367 
368     public static interface NodeMXBean {
369         public String getName();
370         public GraphMXBean getGraph();
371     }
372 
373     public static class Node implements NodeMXBean {
374         public Node(String name) {
375             this.name = name;
376         }
377 
378         public String getName() {
379             return name;
380         }
381 
382         public GraphMXBean getGraph() {
383             return graph;
384         }
385 
386         public void setGraph(Graph graph) {
387             this.graph = graph;
388         }
389 
390         private final String name;
391         private Graph graph;
392     }
393 
394     SimpleType GraphType = SimpleType.OBJECTNAME;
395     GraphMXBean getGraph();
396     void setGraph(GraphMXBean g);
397     GraphMXBean opGraph(GraphMXBean x, GraphMXBean y);
398     String GraphObjectName = "test:type=GraphMXBean";
399     String NodeAObjectName = "test:type=NodeMXBean,name=a";
400     String NodeBObjectName = "test:type=NodeMXBean,name=b";
401     Node NodeA = new Node("a");
402     Node NodeB = new Node("b");
403     GraphMXBean Graph = new Graph(NodeA, NodeB);
404 
405     public static class ExoticCompositeData implements CompositeDataView {
406         private ExoticCompositeData(String whatsit) {
407             this.whatsit = whatsit;
408         }
409 
410         public static ExoticCompositeData from(CompositeData cd) {
411             String whatsit = (String) cd.get("whatsit");
412             if (!whatsit.startsWith("!"))
413                 throw new IllegalArgumentException(whatsit);
414             return new ExoticCompositeData(whatsit.substring(1));
415         }
416 
417         public CompositeData toCompositeData(CompositeType ct) {
418             try {
419                 return new CompositeDataSupport(ct, new String[] {"whatsit"},
420                                                 new String[] {"!" + whatsit});
421             } catch (Exception e) {
422                 throw new RuntimeException(e);
423             }
424         }
425 
426         public String getWhatsit() {
427             return whatsit;
428         }
429 
430         public boolean equals(Object o) {
431             return ((o instanceof ExoticCompositeData) &&
432                     ((ExoticCompositeData) o).whatsit.equals(whatsit));
433         }
434 
435         private final String whatsit;
436 
437         public static final CompositeType type;
438         static {
439             try {
440                 type =
441                     new CompositeType(ExoticCompositeData.class.getName(),
442                                       ExoticCompositeData.class.getName(),
443                                       new String[] {"whatsit"},
444                                       new String[] {"whatsit"},
445                                       new OpenType[] {SimpleType.STRING});
446             } catch (Exception e) {
447                 throw new RuntimeException(e);
448             }
449         }
450     }
451     CompositeType ExoticType = ExoticCompositeData.type;
452     ExoticCompositeData getExotic();
453     void setExotic(ExoticCompositeData ecd);
454     ExoticCompositeData opExotic(ExoticCompositeData ecd1,
455                                  ExoticCompositeData ecd2);
456     ExoticCompositeData Exotic = new ExoticCompositeData("foo");
457 }